/**********************************************************************************
 * Code license:  GNU General Public License v3                                   *
 * Main page:     https://code.google.com/p/library-cross-platform/               *
 * Issue tracker: https://code.google.com/p/library-cross-platform/issues/list    *
***********************************************************************************/

package library.compiler;

import java.lang.reflect.*;
import java.util.List;

import library.parser.*;
import library.util.*;

@SuppressWarnings("rawtypes")
public class BuildManager extends EventHandlerExt {

  protected DirectoryList             dirList              = new DirectoryList();
  protected CheckFileBeforeBuild      checkFileBeforeBuild = null;
  protected List                      projectFileList      = null;
  protected java.lang.String          startFile;

  public BuildManager() {
    registrateEvent(CompilerEventList.COMPILER_EVENT_SEARCH_INCLUDE,          new InclFileListBuild().getClass().getName());
    registrateEvent(CompilerEventList.COMPILER_EVENT_SEARCH_INCLUDE_COMPLETE, new EventSearchIncludeComplete().getClass().getName());
    registrateEvent(CompilerEventList.COMPILER_EVENT_FILE_NOT_FOUND,          new EventFileNotFound().getClass().getName());

    setStartEvent(CompilerEventList.COMPILER_EVENT_SEARCH_INCLUDE);
    
    checkFileBeforeBuild = (CheckFileBeforeBuild)AsciiParser.newInstance(CheckFileBeforeBuild.class, null);
  }
  
  
  public void setDirectory(java.lang.String paths) {
    java.lang.String path[] = paths.split(";");
    int              index  = 0;
    while(index < path.length) {
      dirList.add(path[index]);
      index ++;
    }
  }
  
  public void setStartBuildFile(java.lang.String file) {
    startFile = file;
  }
  
  public void build() {
    InclFileListBuild eventListener = null;

    java.lang.String eventClassName = getEventClassName(CompilerEventList.COMPILER_EVENT_SEARCH_INCLUDE);
    eventListener = (InclFileListBuild)getEventListener(eventClassName, null);
    eventListener.setDirectoryList(dirList);
    eventListener.setMainFunctionFile(startFile);
    setStartEvent(CompilerEventList.COMPILER_EVENT_SEARCH_INCLUDE);
    try {
      startEventStates();
    } catch(InvocationTargetException e) {
        e.printStackTrace();
    } catch(NoSuchMethodException e ) {
        e.printStackTrace();
    } catch(SecurityException e) {
      e.printStackTrace();
    }
  }
  
  protected java.lang.String checkProjectFiles() {
    java.lang.String returnEvent = EventHandlerExt.EVENT_EXIT;
    java.util.ListIterator it = projectFileList.listIterator();
    java.lang.String       file;

    try {
      while(it.hasNext()) {
        file  = (java.lang.String)it.next();
        file = dirList.isPresentFile(file);
        if(file == null) {
         returnEvent = EventHandlerExt.EVENT_EXIT;
         break;
        }
        checkFileBeforeBuild.setFile(file);
        checkFileBeforeBuild.startEventStates();
      }
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (SecurityException e) {
      e.printStackTrace();
    }
    return returnEvent;
  }

  public class EventSearchIncludeComplete extends EventListenerExt {

    public boolean start() throws IllegalEventException {
//      System.out.println("BuildManager.EventSearchIncludeComplete.parse(...)");
      java.lang.String nextevent      = null;
      java.lang.String eventClassName = getEventClassName(CompilerEventList.COMPILER_EVENT_SEARCH_INCLUDE);
      InclFileListBuild eventListener = (InclFileListBuild)getEventListener(eventClassName, null);
      projectFileList = eventListener.getProjectFileList();
      java.util.ListIterator it = projectFileList.listIterator();
      java.lang.String       file;
      
      System.out.println("*** Files to build: ***");
      while(it.hasNext()) {
        file  = (java.lang.String)it.next();
        System.out.println(file);
      }
      System.out.println("**********************************");
      nextevent = checkProjectFiles();
      
      parent.setNextEvent(nextevent);
      return false;
    }
  }

  public class EventFileNotFound extends EventListenerExt {

    public boolean start() throws IllegalEventException {
      System.out.println("*** Files not found event ***");
      System.out.println("*** Exit ***");
      parent.setNextEvent(EventHandlerExt.EVENT_EXIT);
      return false;
    }
 }

}
